package com.gopay.scheduler.job;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.common.constants.cbrmb.CbRmb01727PortalBizStep;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.constants.trans.TxnStaCode;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.domain.cps.intlcard.Tran01742PortalVO;
import com.gopay.common.domain.cps.vo.CPSResponse;
import com.gopay.common.exception.GopayException;
import com.gopay.common.order.dao.CommonQueryDAO;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.common.CallCPSHelper;

/**
 * @ClassName: CbRmbIntlCardRefundSettleJob
 * @Description: 外卡支付退款清算定时任务
 * @author gd.cui
 * @date 2017年12月11日 下午3:05:31
 */
public class CbRmbIntlCardRefundSettleJob extends GopayBaseJob {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource(name = "callCPSHelper")
	private CallCPSHelper callCPSHelper;

	@Resource(name = "commonQueryDAO")
	private CommonQueryDAO commonQueryDAO;

	@Override
	protected void doJobInternal() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		logger.info("-------跨境人民币外卡支付退款做账定时任务开始，时间：" + sdf.format(new Date(System.currentTimeMillis())) + "--------");

		StringBuffer sql = new StringBuffer();
		sql.append("select t.gopay_order_id, t.relevance_gopay_order_id")
		   .append("  from cps_gen_main_order t")
		   .append("  left join cb_rmb_wild_card_assist_dtl a1")
		   .append("    on a1.gopay_order_id = t.relevance_gopay_order_id")
		   .append("  left join cb_rmb_wild_card_assist_dtl a2")
		   .append("    on a2.gopay_order_id = t.gopay_order_id")
		   .append("  left join cps_gen_main_order t1")
		   .append("    on t1.gopay_order_id = t.relevance_gopay_order_id")
		   .append(" where t.gopay_int_txn_cd =:intTxnCd")
		   .append("   and t.txn_sta_cd =:txnStaCd")
		   .append("   and t1.txn_sta_cd =:mainTxnStaCd")
		   .append("   and a1.wild_card_tran_rate is not null")
		   .append("   and a2.wild_card_tran_rate is null");

		Map<String, Object> params = new HashMap<>();
		params.put("intTxnCd", IntTxnCd._01742.value);
		params.put("txnStaCd", TxnStaCode.TXN_STA_CD_30000.value);
		params.put("mainTxnStaCd", TxnStaCode.TXN_STA_CD_20000.value);

		List<Object[]> orders = commonQueryDAO.findBySql(sql.toString(), params);
		if (orders != null && orders.size() > 0) {
			logger.info("------------外卡支付退款清算定时任务，待清算条数：" + orders.size() + "---------------");
			for (Object[] obj : orders) {
				String gopayOrderId = obj[0].toString();
				logger.info("-----------外卡支付退款清算开始，订单号：" + gopayOrderId + "-------------");
				Tran01742PortalVO tranVo = new Tran01742PortalVO();
				tranVo.setBizStep(CbRmb01727PortalBizStep.REFUND_SETTLE_CRON);
				tranVo.setOrderId(gopayOrderId);
				tranVo.setOriginOrderId(obj[1].toString());
				try {
					CPSResponse response = callCPSHelper.callCbRmbByVOTicket(tranVo);
					if (!ProcCodeConstants.PROC_CODE_100S1000.equals(response.getRespCode())) {
						logger.error("-------跨境人民币外卡支付退款做账定时任务失败， RespCode:" + response.getRespCode() + "，订单号：" + gopayOrderId + "--------");
					} else {
						logger.info("-------跨境人民币外卡支付退款做账定时任务成功，订单号：" + gopayOrderId + "--------");
					}
				} catch (GopayException e) {
					logger.error("-------跨境人民币外卡支付退款做账定时任务失败，错误信息：" + e.getErrMsg() + "，订单号：" + gopayOrderId + "--------");
				}
			}
		}

		logger.info("-------跨境人民币外卡支付退款做账定时任务结束，时间：" + sdf.format(new Date(System.currentTimeMillis())) + "--------");
	}

}
